
#include "nane/core/crt/defaultErrorHandler.h"
#include "nane/core/log.h"
#include "nane/core/crt/Backtrace_.h"

namespace nane
{
    ////////////////////////////////////////////////////////////////////////////////////////////////
    void DefaultErrorHandler( const char* _message, const char* _file, const char* _line, const char* _function )
    {
        StackString<512> assertMessage("Assertion failed (%1)\n%2\n%3\n%4\n%5\n%6\n%7\n%8\n%9");
        assertMessage << _message;

        StackFrame* stackFrames = NULL;
        uint32 stackFramesCount = GetStackFrames(&stackFrames, 8);
        for( uint32 i = 0; i < stackFramesCount; ++i )
        {
            assertMessage << (stackFrames[i].text ? stackFrames[i].text : "<source code unavailable>");
        }
        assertMessage.erase(assertMessage.find('%'));
        
        LogManager::Log(assertMessage.c_str(), _file, _line, _function, ELogMessageLevel::Error);
        FreeStackFrames(stackFrames);
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
}   // namespace nane
